package com.ruoyi.video.controller;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.video.domain.Video;
import com.ruoyi.video.domain.VideoVO;
import com.ruoyi.video.serializer.StringToLongArrayDeserializer;
import com.ruoyi.video.service.IVideoService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author Larry
 * @date 2024-03-24
 */
@RestController
public class VideoController extends BaseController
{
    @Resource
    private IVideoService videoService;
    @GetMapping("/getVideoList")
    public R<List<VideoVO>> getList(){
        // 指定日期格式
        List<Video> list = videoService.selectVideoList(null);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        List<VideoVO> res = list.stream().map(
                video -> {
                    VideoVO videoVO = new VideoVO();
                    videoVO.setVideoUrl(video.getFileUrl());
                    videoVO.setId(video.getId());
                    videoVO.setName(video.getName());
                    videoVO.setPicUrl(video.getPicture());// 指定日期格式
                    videoVO.setTime(formatter.format(video.getCreateTime()));
                    return videoVO;
                }
        ).collect(Collectors.toList());
        return R.ok(res);
    }










    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:video:list")
    @GetMapping("/list")
    public TableDataInfo list(Video video)
    {
        startPage();
        List<Video> list = videoService.selectVideoList(video);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:video:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Video video)
    {
        List<Video> list = videoService.selectVideoList(video);
        ExcelUtil<Video> util = new ExcelUtil<Video>(Video.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("system:video:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(videoService.selectVideoById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @RequiresPermissions("system:video:add")
    @PostMapping("/addVideo")
    public AjaxResult add(@RequestBody Video video)
    {
        return toAjax(videoService.insertVideo(video));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:video:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody Video video)
    {
        return toAjax(videoService.updateVideo(video));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:video:remove")
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable("ids") String idString) {
        logger.info(idString);
        Long[] ids = Arrays.stream(idString.split(","))
                .map(idS -> Long.parseLong(idS.trim()))
                .toArray(Long[]::new);
        for (Long id : ids) {
            System.out.println(id);
        }
        return toAjax(videoService.deleteVideoByIds(ids));
    }
    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:video:remove")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id)
    {
        logger.info(id.toString());
        return toAjax(videoService.deleteVideoById(id));
    }
}
